package com.example.myapplication6;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import android.view.Window;

public class MainActivity extends Activity {
    private WebView wv;
    private ListView lv;
    private SharedPreferences share;
    private ArrayList<String> history = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_PROGRESS); // 启用进度条
        setContentView(R.layout.activity_main);

        // 初始化组件
        wv = findViewById(R.id.wv);
        lv = findViewById(R.id.lv);
        EditText et = findViewById(R.id.et);
        Button btn = findViewById(R.id.btn);
        Button btnForward = findViewById(R.id.btnForward);
        Button btnBack = findViewById(R.id.btnBack);

        // 加载历史记录
        share = getSharedPreferences("com.sise.browser.history", MODE_PRIVATE);
        int count = share.getInt("count", 0);
        String[] urls = new String[count];
        for (int i = 0; i < count; i++) {
            urls[i] = share.getString("url_" + i, "");
            history.add(urls[i]);
        }
        ArrayAdapter<String> ad = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, urls);
        lv.setAdapter(ad);

        // 设置WebView
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url); // 在当前WebView打开链接
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if (!history.contains(url)) {
                    history.add(url); // 添加到历史记录
                }
            }
        });

        // GO按钮事件
        btn.setOnClickListener(v -> {
            lv.setVisibility(View.GONE);
            wv.setVisibility(View.VISIBLE);

            String url = et.getText().toString().trim();
            if (URLUtil.isNetworkUrl(url)) {
                wv.loadUrl(url);
            } else {
                Toast.makeText(this, "网址格式错误", Toast.LENGTH_SHORT).show();
                et.requestFocus();
            }
        });

        // 前进/后退按钮
        btnForward.setOnClickListener(v -> {
            if (wv.canGoForward()) {
                wv.goForward();
            } else {
                Toast.makeText(this, "无法前进", Toast.LENGTH_SHORT).show();
            }
        });

        btnBack.setOnClickListener(v -> {
            if (wv.canGoBack()) {
                wv.goBack();
            } else {
                Toast.makeText(this, "无法后退", Toast.LENGTH_SHORT).show();
            }
        });

        // 历史记录点击事件
        lv.setOnItemClickListener((parent, view, position, id) -> {
            String url = urls[position];
            et.setText(url);
            wv.loadUrl(url);
            lv.setVisibility(View.GONE);
            wv.setVisibility(View.VISIBLE);
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 保存历史记录
        SharedPreferences.Editor editor = share.edit();
        editor.clear();
        for (int i = 0; i < history.size(); i++) {
            editor.putString("url_" + i, history.get(i));
        }
        editor.putInt("count", history.size());
        editor.apply();
    }
}